package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.douwong.student.R;
import com.douwong.student.activity.HandWrittenImagView;
import com.douwong.student.adapter.AnswerExhangeAdapter;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.db.HandExchangeDBService;
import com.douwong.student.model.AnswerRecord;
import com.douwong.student.view.PullToRefreshListView;

import java.io.File;
import java.util.ArrayList;

/**
 * 手写互动碎片  手写互动记录
 * @author leihuajie
 *
 */
@SuppressLint("ValidFragment")
public class HandExchangeFragment extends Fragment {

	private PullToRefreshListView hand_listview;
	private AnswerExhangeAdapter adapter;
	private View view ;
	private ArrayList<AnswerRecord>list;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.hand_exchange_layout, null);
		hand_listview=(PullToRefreshListView) view.findViewById(R.id.hand_listview);
		list=new ArrayList<AnswerRecord>();
		
		if(BaseApplication.sUserBean!=null){
			 HandExchangeDBService.getInstance(getActivity()).query(list);
		}
		adapter=new AnswerExhangeAdapter(getActivity(), 1,list);
		hand_listview.setAdapter(adapter);
		hand_listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					Intent intent=new Intent();
					intent.setClass(getActivity(), HandWrittenImagView.class);
					intent.putExtra("OnlyRead", "OnlyRead");
					intent.putExtra("filePath", list.get(position-1).getFilePath());
					File file=new File(list.get(position-1).getFilePath());
					if(file.exists()){
						startActivity(intent);
					}else {
						Toast.makeText(getActivity(), "文件不存在", Toast.LENGTH_LONG).show();
						HandExchangeDBService.getInstance(getActivity()).delete(list.get(position-1).getTitleName());
						list.remove(position-1);
						adapter.notifyDataSetChanged();
					}
					
//					FileUtils.openFile(list.get(position).getFilePath(), list.get(position).getTitleName(), getActivity());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		hand_listview.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				hand_listview.onScrollStateChanged(view, scrollState);
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				hand_listview.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
				
			}
		});
		hand_listview.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				list.clear();
				if(BaseApplication.sUserBean!=null){
					list=(HandExchangeDBService.getInstance(getActivity()).query(list));
				}
				adapter.notifyDataSetChanged();
				hand_listview.onRefreshComplete();
			}


		});
		return view;
	}
}
